# -*- coding: utf-8 -*-
# @Time     :2021/10/25 15:41
# @Author   : master
# @Software : PyCharm


from openpyxl import load_workbook
from tools.read_config import ReadConfig
from tools.project_path import *
from tools.get_data import *


class DoExcel():
    @classmethod
    def get_data(cls, file_name):
        wb = load_workbook(file_name)
        mode = eval(ReadConfig.get_config(case_config_path, 'MODE', 'mode'))
        tel = getattr(GetData, 'NoRegTel')      # 从GetData里面拿到的数据
        # 利用python查询数据库的方式 去拿到最大的手机号--这里可以加 也可放到get_data
        test_data = []
        for key in mode:        # 遍历这个存在配置文件里面的字典
            sheet = wb[key]         # 表单名
            if mode[key] == 'all':
                for i in range(2, sheet.max_row + 1):
                    row_data = {}
                    row_data['case_id'] = sheet.cell(
                        i, 1).value              # 行号
                    row_data['url'] = sheet.cell(i, 3).value
                    row_data['header'] = sheet.cell(i, 4).value
                    # row_data['data'] = sheet.cell(i, 5).value
                    if sheet.cell(i, 5).value.find(
                            '${tel_1}') != -1:     # 有找到这个字符串
                        row_data['data'] = sheet.cell(
                            i, 5).value.replace(
                            '${tel_1}', str(tel))
                    elif sheet.cell(i, 5).value.find('${tel}') != -1:
                        row_data['data'] = sheet.cell(
                            i, 5).value.replace(
                            '${tel}', str(
                                tel + 1))
                    else:                        # 没有找到需替换的字符串
                        row_data['data'] = sheet.cell(i, 5).value
                    row_data['title'] = sheet.cell(i, 6).value
                    row_data['http_method'] = sheet.cell(i, 2).value
                    row_data['expected'] = sheet.cell(
                        i, 7).value     # 添加一个期望值到测试数据中去
                    row_data['sheet_name'] = key
                    test_data.append(row_data)
                    cls.update_tel(
                        tel + 2, file_name, 'init')     # 更新手机号  针对Excel的操作
            else:
                for case_id in mode[key]:
                    row_data = {}    # 字典
                    row_data['case_id'] = sheet.cell(
                        case_id + 1, 1).value  # 行号
                    row_data['url'] = sheet.cell(case_id + 1, 3).value
                    row_data['header'] = sheet.cell(case_id + 1, 4).value
                    # row_data['data'] = sheet.cell(case_id + 1, 5).value
                    if sheet.cell(case_id + 1, 5).value.find(
                            '${tel_1}') != -1:  # 有找到这个字符串
                        row_data['data'] = sheet.cell(
                            case_id + 1, 5).value.replace(
                            '${tel_1}', str(tel))
                    elif sheet.cell(case_id, 5).value.find('${tel}') != -1:
                        row_data['data'] = sheet.cell(
                            case_id + 1, 5).value.replace(
                            '${tel}', str(
                                tel + 1))
                    else:
                        row_data['data'] = sheet.cell(case_id + 1, 5).value
                    row_data['title'] = sheet.cell(case_id + 1, 6).value
                    row_data['http_method'] = sheet.cell(case_id + 1, 2).value
                    row_data['expected'] = sheet.cell(
                        case_id + 1, 7).value  # 添加一个期望值到测试数据中去
                    row_data['sheet_name'] = key
                    test_data.append(row_data)
                    cls.update_tel(tel + 2, file_name, 'init')
        return test_data

    @classmethod
    def write_back(self, file_name, sheet_name, i, result, TestResult, response_time):           # 写回数据
        wb = load_workbook(file_name)
        sheet = wb[sheet_name]
        sheet.cell(i, 8).value = result
        sheet.cell(i, 9).value = TestResult
        sheet.cell(i, 10).value = response_time
        wb.save(file_name)                                     # 保存结果

    @classmethod
    def update_tel(cls, tel, filename, sheet_name):
        '''跟新Excel里面手机号的数据'''
        wb = load_workbook(filename)
        sheet = wb[sheet_name]
        wb['init'].cell(1, 2).value = tel       # 赋值操作
        wb.save(filename)


if __name__ == '__main__':
    test_data = DoExcel().get_data('../test_data/test_data.xlsx')
    print(test_data)
